import cv2
import numpy as np

img = cv2.imread("img/salt_pepper_noise_example.png")
# print(img.shape)

# 定义卷积核. 3*3的卷积核，共9各像素，所以用1/9去乘以每个像素值
kernel = np.array([
    [1 / 9, 1 / 9, 1 / 9],
    [1 / 9, 1 / 9, 1 / 9],
    [1 / 9, 1 / 9, 1 / 9]
])

result = cv2.filter2D(img, -1, kernel)
cv2.imshow("result", result)
cv2.waitKey(0)
